package com.have.blog.xo.service.admin.impl;


import com.github.pagehelper.PageHelper;
import com.have.blog.base.constant.ErrorCode;
import com.have.blog.base.enums.BlogStatus;
import com.have.blog.base.exception.exceptionType.QueryException;
import com.have.blog.model.api.CommonPage;
import com.have.blog.model.constant.SQLConstant;
import com.have.blog.model.mapper.TSysParamsMapper;
import com.have.blog.model.models.TSysParams;
import com.have.blog.model.models.TSysParamsExample;
import com.have.blog.utils.MyStringUtils;
import com.have.blog.utils.ResultUtils;
import com.have.blog.xo.constant.MessageConstant;
import com.have.blog.xo.service.admin.SystemParamService;
import com.have.blog.xo.service.cache.AdminSystemParamCacheService;
import com.have.blog.xo.vo.SystemParamVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@SuppressWarnings("all")
public class SystemParamServiceImpl implements SystemParamService {
    @Autowired
    private TSysParamsMapper sysParamsMapper;
    @Autowired
    private AdminSystemParamCacheService systemParamCacheService;

    /**
     * 通过关键词获取系统参数的值
     * @param paramKey
     * @return
     */
    @Override
    public String getSystemParamValueByKey(String paramKey) {

        TSysParams sysParams = systemParamCacheService.getSystemParamByParamKey(paramKey);
        if (sysParams != null) {
            return sysParams.getParamsValue();
        }
        TSysParamsExample example = new TSysParamsExample();
        example.createCriteria()
                .andParamsKeyEqualTo(paramKey);
        List<TSysParams> tSysParams = sysParamsMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(tSysParams)) {
            throw new QueryException(ErrorCode.PLEASE_CONFIGURE_SYSTEM_PARAMS, MessageConstant.PLEASE_CONFIGURE_SYSTEM_PARAMS);
        }
        sysParams = tSysParams.get(0);
        systemParamCacheService.saveSystemParamByParamKey(sysParams, paramKey, 1, TimeUnit.DAYS);
        return sysParams.getParamsValue();
    }

    @Override
    public String getPageList(SystemParamVo sysParamsVO) {
        PageHelper.startPage(sysParamsVO.getCurrentPage(), sysParamsVO.getPageSize());
        TSysParamsExample example = new TSysParamsExample();
        TSysParamsExample.Criteria criteria = example.createCriteria()
                                                    .andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        if (!StringUtils.isEmpty(sysParamsVO.getParamsName()) && !StringUtils.isEmpty(sysParamsVO.getParamsName().trim())){
            criteria.andParamsNameLike("%" + sysParamsVO.getParamsName().trim() + "%");
        }
        if (!StringUtils.isEmpty(sysParamsVO.getParamsKey()) && !StringUtils.isEmpty(sysParamsVO.getParamsKey().trim())) {
            criteria.andParamsKeyLike("%" + sysParamsVO.getParamsKey() + "%");
        }

        example.setOrderByClause("create_time desc");
        List<TSysParams> sysParamList = sysParamsMapper.selectByExample(example);
        return ResultUtils.successWithData(CommonPage.restPage(sysParamList));
    }

    @Override
    public String addSysParams(SystemParamVo sysParamsVO) {
        TSysParamsExample example = new TSysParamsExample();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andParamsKeyEqualTo(sysParamsVO.getParamsKey());
        List<TSysParams> tSysParams = sysParamsMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(tSysParams)) {
            return ResultUtils.errorWithMessage(MessageConstant.ENTITY_EXIST);
        }
        TSysParams sysParams = new TSysParams();
        BeanUtils.copyProperties(sysParamsVO, sysParams, SQLConstant.STATUS);
        sysParams.setStatus(BlogStatus.ENABLE.getStatus());
        sysParams.setUid(MyStringUtils.getUUID());
        int result = sysParamsMapper.insert(sysParams);
        if (result > 0) {
            return ResultUtils.successWithMessage(MessageConstant.INSERT_SUCCESS);
        }
        return ResultUtils.errorWithMessage(MessageConstant.INSERT_FAIL);
    }

    @Override
    public String editSysParams(SystemParamVo sysParamsVO) {
        TSysParams sysParams = sysParamsMapper.selectByPrimaryKey(sysParamsVO.getUid());
        if (sysParams == null) {
            return ResultUtils.errorWithMessage(MessageConstant.ENTITY_NOT_EXIST);
        }
        sysParams.setParamsKey(sysParamsVO.getParamsKey());
        sysParams.setParamsName(sysParamsVO.getParamsName());
        sysParams.setParamsValue(sysParamsVO.getParamsValue());
        sysParams.setParamsType(sysParamsVO.getParamsType());
        sysParams.setRemark(sysParamsVO.getRemark());
        sysParams.setSort(sysParamsVO.getSort());
        sysParams.setUpdateTime(new Date());
        int result = sysParamsMapper.updateByPrimaryKeySelective(sysParams);
        if (result > 0) {
            return ResultUtils.successWithMessage(MessageConstant.UPDATE_SUCCESS);
        }
        return ResultUtils.errorWithMessage(MessageConstant.UPDATE_FAIL);
    }

    @Override
    public String deleteBatchSysParams(List<SystemParamVo> sysParamsVoList) {
        if (CollectionUtils.isEmpty(sysParamsVoList)) {
            return ResultUtils.successWithMessage(MessageConstant.DELETE_SUCCESS);
        }
        List<String> uids = new ArrayList<>();
        sysParamsVoList.forEach(item -> {
            uids.add(item.getUid());
        });
        TSysParamsExample example = new TSysParamsExample();
        example.createCriteria()
                .andUidIn(uids);
        TSysParams sysParams = new TSysParams();
        sysParams.setStatus(BlogStatus.DISABLE.getStatus());
        int result = sysParamsMapper.updateByExampleSelective(sysParams, example);
        if (result > 0) {
            return ResultUtils.successWithMessage(MessageConstant.DELETE_SUCCESS);
        }
        return ResultUtils.errorWithMessage(MessageConstant.DELETE_FAIL);
    }
}
